package net.caixiaomi.info.ui.matching;

import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sobot.chat.utils.DateUtil;
import com.sobot.chat.utils.ScreenUtils;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.model.MatchingItem;
import net.caixiaomi.info.util.Constants;

import java.util.List;

/**
 * Created by suntian on 2018/7/20.
 */

public class MatchItemAdapter extends BaseMultiItemQuickAdapter<MatchingItem, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *Ò
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MatchItemAdapter(List<MatchingItem> data,CollcetCallBack callBack) {
        super(data);
        mCollectCallback=callBack;
        addItemType(Constants.ITEM_NEWS_MATHING, R.layout.item_matching_layout);
        addItemType(Constants.ITEM_VIEW_HEADER, R.layout.header_football);
        addItemType(Constants.ITEM_VIEW_EMPTY, R.layout.no_match_layout);
    }

    @Override
    protected void convert(BaseViewHolder helper, MatchingItem item) {
        try {
            switch (helper.getItemViewType()) {
                case Constants.ITEM_VIEW_HEADER:
                    bindHead(helper, item);
                    break;
                case Constants.ITEM_NEWS_MATHING:
                    bindItem(helper, item);
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void bindHead(BaseViewHolder helper, MatchingItem item) {
        helper.setGone(R.id.image, false);
        helper.setGone(R.id.btn_expand, false);
//        String text = CommonApp.getInstance().getString(R.string.C_MATCH_RESULT_TOTAL_COUNT, item.getTotalCount(), item.getFinishCount());
//        SpannableString spannableString = new SpannableString(text);
//        spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), text.length() - 1 - String.valueOf(item.getFinishCount()).length(), text.length() - 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
//        helper.setText(R.id.title, spannableString);
//        helper.setText(R.id.title, "++++");

//        String html="<html><body>2018-07-26 周五共有<font color='red'>8</font>场比赛</body></html>";
        TextView title=helper.getView(R.id.title);
//        title.setText(Html.fromHtml(html));
        title.setText(Html.fromHtml(item.getMatchDateStr()));
    }

    private void bindItem(BaseViewHolder helper, MatchingItem item) {
        try {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(item.getChangci()).append(" ").append(item.getLeagueAddr()).append(" ").append(item.getMatchTime());

            helper.setText(R.id.text, stringBuilder);

            helper.setText(R.id.host, item.getHomeTeamAbbr());
            helper.setText(R.id.guest, item.getVisitingTeamAbbr());

            helper.setBackgroundRes(R.id.matching_item_collect_iv,TextUtils.equals(item.getIsCollect(),"0")?R.mipmap.ic_collect_normal:R.mipmap.ic_collected);

            stringBuilder.setLength(0);
//            matchfinish ：比赛状态:0-未开赛,1-已完成,2-取消,4-推迟,5-暂停,6-进行中 ,
//            if (TextUtils.equals(item.getMatchFinish(), "2")) {
//                stringBuilder.append(CommonApp.getInstance().getString(R.string.C_NO_START_MATCH));
//            } else if (TextUtils.equals(item.getMatchFinish(), "0") || TextUtils.equals(item.getMatchFinish(), "1")) {
//                if (!TextUtils.isEmpty(item.getFirstHalf())) {
//                    stringBuilder.append(CommonApp.getInstance().getString(R.string.C_HALF_MATCH)).append(" ").append(item.getFirstHalf()).append("     ");
//                }
//                if (!TextUtils.isEmpty(item.getWhole())) {
//                    stringBuilder.append(CommonApp.getInstance().getString(R.string.C_ALL_SCORE)).append(item.getWhole());
//                }
//            }

            //-------------
//            item.setMatchFinish("0");
            String showTime=item.getMatchTimeStart();
            long time=Long.parseLong(showTime)*1000;
            String timer=DateUtil.toDate(time,DateUtil.DATE_FORMAT0);

            if(item.getMatchFinish().equals("0")||item.getMatchFinish().equals("2")||item.getMatchFinish().equals("4")||item.getMatchFinish().equals("5")){
                helper.setTextColor(R.id.info, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
            }else{
                helper.setTextColor(R.id.info, ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary));
            }

            if (TextUtils.equals(item.getMatchFinish(), "0")) {
                stringBuilder.append(CommonApp.getInstance().getString(R.string.C_NO_START_MATCH)).append("         ").append(timer);
            }else if(TextUtils.equals(item.getMatchFinish(), "2")){
                stringBuilder.append("取消").append("         ").append(timer);
            } else if(TextUtils.equals(item.getMatchFinish(), "4")){
                stringBuilder.append("推迟").append("         ").append(timer);
            }else if(TextUtils.equals(item.getMatchFinish(), "5")){
                stringBuilder.append("暂停").append("         ").append(timer);
            }else if (TextUtils.equals(item.getMatchFinish(), "1")) {
                if (!TextUtils.isEmpty(item.getWhole())) {
                    stringBuilder.append(CommonApp.getInstance().getString(R.string.C_ALL_SCORE)).append(item.getWhole()).append("     ");
                }
                if (!TextUtils.isEmpty(item.getFirstHalf())) {
                    stringBuilder.append(CommonApp.getInstance().getString(R.string.C_HALF_MATCH)).append(" ").append(item.getFirstHalf());
                }

            }else if(TextUtils.equals(item.getMatchFinish(), "6")){
                stringBuilder.append(item.getMinute()).append("′         ").append(item.getWhole());
            }



            //------------

            String info = stringBuilder.toString().trim();

            helper.setGone(R.id.info, !TextUtils.isEmpty(info));
            helper.setText(R.id.info, info);
            RequestOptions options = new RequestOptions();
            options.error(R.mipmap.ic_football);
            options.placeholder(R.mipmap.ic_football);
            Glide.with(CommonApp.getInstance()).load(item.getHomeTeamLogo()).apply(options).into((ImageView) helper.getView(R.id.icon_host));
            Glide.with(CommonApp.getInstance()).load(item.getVisitingTeamLogo()).apply(options).into((ImageView) helper.getView(R.id.icon_guest));

            helper.getView(R.id.matching_item_collect_iv_group).setTag(item);
            helper.getView(R.id.matching_item_collect_iv_group).setOnClickListener(mCollectListener);

            boolean dealVersion = CommonApp.STORE.getBoolean(Constants.KEY_DEAL_VERSION, false);
            if(mContext.getString(R.string.H5_SAFE_INSURANCE).equals("0")) {
                helper.setGone(R.id.matching_item_go_iv, dealVersion);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private View.OnClickListener mCollectListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MatchingItem item= (MatchingItem) view.getTag();
            mCollectCallback.collectClick(item);
        }
    };

    public interface CollcetCallBack{
        public void collectClick(MatchingItem item);
    }

    public CollcetCallBack mCollectCallback;

}
